package com.digitalchina.lxnsy.activity.old;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.apkfuns.logutils.LogUtils;
import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.adapter.ShareCity1Adapter;
import com.digitalchina.lxnsy.api.BusinessShareFarm;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.ShareCitysVO;
import com.digitalchina.lxnsy.entity.ShareSelectCityVO;
import com.digitalchina.lxnsy.interfaces.ICheckBox;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;

import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2018/8/16 14:34
 * 修改备注：
 * ================================================
 */
public class ShareSelectCityActivity extends BaseActivity implements ICheckBox {

    @Bind(R.id.btn_save)
    Button btnSave;
    @Bind(R.id.lv1)
    ListView lv1;
    @Bind(R.id.lv2)
    ListView lv2;
    @Bind(R.id.lv3)
    ListView lv3;
    @Bind(R.id.tv_all_city)
    TextView tvAllCity;
    @Bind(R.id.layout_list)
    LinearLayout layoutList;

    private ShareCity1Adapter adapter1, adapter2, adapter3;

    private int p1; // 第1个list下标
    private int p2; // 第2个list下标

    private String productId = "";

    private List<ShareCitysVO> voList = new ArrayList<>();
    private List<ShareCitysVO> emptyList = new ArrayList<>();
    private boolean isShowChecked = true;

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_share_scope_select);
        setTitle("共享范围");
    }

    @Override
    public void initMember() {

        //isDetail==true 时 从详情跳转过来 不显示checkbox
        //为false 时 从新增页面跳转过来 按需判断 productId 是否为空

        if (getIntent().hasExtra("map")) {
            Map<String, Object> map = (Map<String, Object>) getIntent().getSerializableExtra("map");
            productId = map.get("productId").toString();
            boolean isDetail = (boolean) map.get("isDetail");

            if (isDetail) {
                BusinessShareFarm.getRegions(this, productId, true, mHandler);
                isShowChecked = false;
            } else {
                btnSave.setVisibility(View.VISIBLE);
                getAssetsText();
            }

        }


        adapter1 = new ShareCity1Adapter(ShareSelectCityActivity.this, emptyList, ShareSelectCityActivity.this, 1, isShowChecked);
        lv1.setAdapter(adapter1);
        adapter2 = new ShareCity1Adapter(ShareSelectCityActivity.this, emptyList, ShareSelectCityActivity.this, 2, isShowChecked);
        lv2.setAdapter(adapter2);
        adapter3 = new ShareCity1Adapter(ShareSelectCityActivity.this, emptyList, ShareSelectCityActivity.this, 3, isShowChecked);
        lv3.setAdapter(adapter3);
    }

    private void getAssetsText() {

        showDialog("正在加载数据，请稍候...");

        new Thread(new Runnable() {
            @Override
            public void run() {

                LogUtils.e("1----读取本地文件");
                InputStream inputStream = null;
                try {
                    inputStream = getAssets().open("shareCity.txt");
                    int size = inputStream.available();
                    int len = -1;
                    byte[] bytes = new byte[size];
                    inputStream.read(bytes);
                    inputStream.close();
                    String string = new String(bytes);
                    LogUtils.e("2----读取完毕,转换voList");
                    voList = FastJsonUtil.getListBean(string, ShareCitysVO.class);

                } catch (IOException e) {
                    e.printStackTrace();
                }

                ShareSelectCityActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        dismissDialog();
                        LogUtils.e("3----渲染页面");
                        adapter1.setSelectPosition(-1);
                        adapter1.setDatas(voList);
                        layoutList.setVisibility(View.VISIBLE);
                        LogUtils.e("4----完成");
                    }
                });
            }
        }).start();


    }

    @Override
    public void addListener() {

        lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                p1 = i;
                p2 = -1;
                adapter1.setSelectPosition(p1);
                adapter2.setSelectPosition(p2);
                adapter2.setDatas(voList.get(p1).getChildrenList());
                adapter3.setDatas(emptyList);
            }
        });

        lv2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                p2 = i;
                adapter2.setSelectPosition(p2);
                adapter3.setDatas(voList.get(p1).getChildrenList().get(p2).getChildrenList());
            }
        });

        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                List<ShareSelectCityVO> tempList1 = new ArrayList<>();
                List<ShareSelectCityVO> tempList2 = new ArrayList<>();
                List<ShareSelectCityVO> tempList3 = new ArrayList<>();


                for (int i = 0; i < voList.size(); i++) {
                    if (voList.get(i).isChecked()) {
                        ShareSelectCityVO vo1 = new ShareSelectCityVO(); // 省
                        vo1.setProvinceChecked("true");
                        vo1.setProvinceName(voList.get(i).getName());
                        vo1.setProvinceCode(voList.get(i).getCode());

                        for (int j = 0; j < voList.get(i).getChildrenList().size(); j++) {
                            if (voList.get(i).getChildrenList().get(j).isChecked()) {
                                ShareSelectCityVO vo2 = new ShareSelectCityVO(); // 市
                                vo2.setProvinceChecked("false");
                                vo2.setProvinceName(voList.get(i).getName());
                                vo2.setProvinceCode(voList.get(i).getCode());
                                vo2.setCityChecked("true");
                                vo2.setCityName(voList.get(i).getChildrenList().get(j).getName());
                                vo2.setCityCode(voList.get(i).getChildrenList().get(j).getCode());

                                for (int k = 0; k < voList.get(i).getChildrenList().get(j).getChildrenList().size(); k++) {
                                    if (voList.get(i).getChildrenList().get(j).getChildrenList().get(k).isChecked()) {
                                        ShareSelectCityVO vo3 = new ShareSelectCityVO(); // 区
                                        vo3.setProvinceChecked("false");
                                        vo2.setProvinceName(voList.get(i).getName());
                                        vo2.setProvinceCode(voList.get(i).getCode());
                                        vo3.setCityChecked("false");
                                        vo2.setCityName(voList.get(i).getChildrenList().get(j).getName());
                                        vo2.setCityCode(voList.get(i).getChildrenList().get(j).getCode());
                                        vo3.setCountyName(voList.get(i).getChildrenList().get(j).getChildrenList().get(k).getName());
                                        vo3.setCountyCode(voList.get(i).getChildrenList().get(j).getChildrenList().get(k).getCode());
                                        tempList3.add(vo3);
                                    }
                                }

                                if (tempList3.size() == voList.get(i).getChildrenList().get(j).getChildrenList().size()) {
                                    tempList2.add(vo2);
                                    tempList3.clear();
                                } else {
                                    tempList2.addAll(tempList3);
                                    tempList3.clear();
                                }
                            }

                        }

                        if (tempList2.size() == voList.get(i).getChildrenList().size()) {
                            tempList1.add(vo1);
                            tempList2.clear();
                        } else {
                            tempList1.addAll(tempList2);
                            tempList2.clear();
                        }
                    }


                }

                Intent intent = new Intent();
                intent.putExtra("list", (Serializable) tempList1);
                setResult(RESULT_OK, intent);
                finish();

            }
        });
    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);

                switch (msg.what) {
                    case MSG.MSG_GET_REGIONS_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_GET_REGIONS_FIELD:
                        showToast(msg.obj.toString());
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(final Object object) {

        showDialog("正在加载数据，请稍候...");

        new Thread(new Runnable() {
            @Override
            public void run() {
                voList = FastJsonUtil.getListBean(object.toString(), ShareCitysVO.class);
                ShareSelectCityActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        dismissDialog();
                        if (voList == null || voList.size() == 0) {
                            tvAllCity.setVisibility(View.VISIBLE);
                        } else {
                            layoutList.setVisibility(View.VISIBLE);
                            adapter1.setSelectPosition(-1);
                            adapter1.setDatas(voList);
                        }
                    }
                });
            }
        }).start();


    }

    @Override
    public void rightOnClick() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void checked(boolean isChecked, int type, int index) {


        switch (type) {
            case 1:
                p1 = index;
                p2 = -1;
                voList.get(p1).setChecked(isChecked);
                for (int i = 0; i < voList.get(p1).getChildrenList().size(); i++) {
                    voList.get(p1).getChildrenList().get(i).setChecked(isChecked);
                    for (int j = 0; j < voList.get(p1).getChildrenList().get(i).getChildrenList().size(); j++) {
                        voList.get(p1).getChildrenList().get(i).getChildrenList().get(j).setChecked(isChecked);
                    }
                }
                adapter1.setSelectPosition(p1);
                adapter2.setSelectPosition(p2);
                adapter1.setDatas(voList);
                if (adapter2.getCount() == 0)
                    adapter2.setDatas(voList.get(p1).getChildrenList());
                else
                    adapter2.notifyDataSetChanged();
                break;
            case 2:
                p2 = index;
                voList.get(p1).getChildrenList().get(p2).setChecked(isChecked);
                for (int i = 0; i < voList.get(p1).getChildrenList().get(p2).getChildrenList().size(); i++) {
                    voList.get(p1).getChildrenList().get(p2).getChildrenList().get(i).setChecked(isChecked);
                }
                adapter2.notifyDataSetChanged();
                if (adapter3.getCount() == 0)
                    adapter3.setDatas(voList.get(p1).getChildrenList().get(p2).getChildrenList());
                else
                    adapter3.notifyDataSetChanged();
                break;
            case 3:
                voList.get(p1).getChildrenList().get(p2).getChildrenList().get(index).setChecked(isChecked);
                break;
        }

    }
}
